<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/14
 * Time: 22:30
 */
namespace app\admin\controller;

use \app\admin\model\Goodstype as GoodstypeModel;
use org\Tree;


class Goodstype extends Base
{
    //菜单列表
    public function index()
    {
        if (request()->isAjax()){
            $where = [];
            $goodsCaseModel = new GoodstypeModel();
            $goodsCaseList = $goodsCaseModel->getGoodsCaseList($where);

            foreach($goodsCaseList as $key=>$vo){
                $goodsCaseList[$key]['img'] = '<img src="' . $vo['img'] . '" width="40px" height="40px">';
                $goodsCaseList[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }
            Tree::instance()->init(objToArray($goodsCaseList));
            $goodsCaseList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');

            $return['rows'] = $goodsCaseList;
            return json($return);
        }
        return $this->fetch();
    }

    // 添加菜单
    public function add()
    {
        $goodsCaseModel = new GoodstypeModel();
        if(request()->isPost()){
            $param = input('post.');
            unset($param['file']);
            $flag = $goodsCaseModel->goodscaseAdd($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $goodsCaseList = $goodsCaseModel->getGoodsCaseList([]);
        Tree::instance()->init(objToArray($goodsCaseList));
        $goodsCaseList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
        $this->assign('goodscas',$goodsCaseList);
        return $this->fetch();
    }

    // 编辑节点
    public function edit()
    {
        $goodsCaseModel = new GoodstypeModel();
        if(request()->isAjax()) {
            $param = input('post.');
            $flag = $goodsCaseModel->goodscaseEdie($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }


        $goodsCaseList = $goodsCaseModel->getGoodsCaseList([]);
        Tree::instance()->init(objToArray($goodsCaseList));
        $goodsCaseList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
        $this->assign('goodscas',$goodsCaseList);
        $this->assign('goodscaseinfo',db('fenlei')->where('id',$this->request->param('id'))->find());

        return $this->fetch();
    }

    public function doedit(){
        $goodsCaseModel = new GoodstypeModel();
        if(request()->isAjax()) {
            $param = input('post.');
            $flag = $goodsCaseModel->goodscaseEdie($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
    }

    // 删除节点
    public function Del()
    {
        $id = input('param.id');

        $flag = (new GoodstypeModel())->goodscaseDel($id);

        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'goodstype/edit',
                'href' => url('goodstype/edit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'goodstype/del',
                'href' => "javascript:goodscaseDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }

}